#include "inflash.h"
#include <string.h>  
uint32_t temp1,temp2;

/*******************************************************************************
* Function Name  : FLASH_Init
* Description    : Init the FLASH to Program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void BD_INFLASH_Init(void)
{
//  /* Unlock the Flash Program Erase controller */
//  FLASH_Unlock();
//  /* Clear All pending flags */
//  FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
//                  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
//    HAL_FLASH_Unlock();    
}
/**
 *  FlashAddress:写入的开始地址
 *  DataAddr:写入的数据地址，4字节地址
 *  DataLength:写入的数据长度，字节单位
 **/
uint32_t InFLASH_Write(uint32_t FlashAddress, uint32_t *DataAddr ,uint32_t DataLength)
{
    uint32_t i=0,DataLength_Conv=0;
    uint32_t* srcAddr = DataAddr;
    //转换需要写入的数据（256bit）长度 不足256bit/32Byte的数据则长度需要补1
    if(DataLength%4)
    {
        DataLength_Conv=1;
    }
    DataLength_Conv+=DataLength/4;
    
    HAL_FLASH_Unlock();             //解锁
    for (i = 0; (i < DataLength_Conv) && (FlashAddress <= (FLASH_END_ADDR-4)); i++)
    {
        /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
             be done by word */
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,FlashAddress, *srcAddr) == HAL_OK)       //一次写入32Byte数据
        {
            /* Check the written value */
                temp1 = *(uint32_t*)FlashAddress;
                temp2 = *srcAddr++;
                if(temp1 != temp2)
                {
                    HAL_FLASH_Lock();
                    return(2);
                }
                FlashAddress+=4;
                DataAddr++;             //32bit
        }
        else
        {
        /* Error occurred while writing data in Flash memory */
            HAL_FLASH_Lock();
            return (1);
        }
    }
    HAL_FLASH_Lock();
    return (0);
}

/**
 *  FlashAddress:读取的开始地址
 *  pData:读取的数据地址，4字节地址
 *  DataLength:读取的数据长度，字节单位
 **/
void InFLASH_Read(uint32_t FlashAddress, uint32_t* pData, uint32_t DataLength)
{
    uint32_t i;
    __IO uint32_t  *pFlash;

    pFlash = (__IO uint32_t  *)FlashAddress;
    memset(pData, 0xFFFFFFFF, DataLength);
    pFlash = (__IO uint32_t  *) FlashAddress;
    for(i=0;i<DataLength/4;i++)
        *pData++ = *pFlash++;
}

HAL_StatusTypeDef InFlash_Erase(uint32_t startAddr,size_t len)
{
    HAL_StatusTypeDef status = HAL_ERROR;
    FLASH_EraseInitTypeDef eraseInit;
    uint32_t startSector = FLASH_SECTOR_5;
    uint32_t sectorCount;
    uint32_t sectorErr;
    if(startAddr < APP_ADDR)      // 保护boot区域
    {
        startAddr = APP_ADDR;
    }
    if(startAddr >= FLASH_EEP_ADDR)          // 保护eeprom区域
    {
        return status;
    }
    startSector += (startAddr - APP_ADDR)/APP_SECTOR_SIZE;
    sectorCount = (startAddr + len)/APP_SECTOR_SIZE-startAddr/APP_SECTOR_SIZE;

    if(startSector + sectorCount > FLASH_SECTOR_10)  // 保护eeprom区域
    {
        sectorCount = FLASH_SECTOR_10 - startSector;
    }
    eraseInit.Sector = startSector;
    eraseInit.NbSectors = sectorCount;
    eraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
    eraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
    HAL_FLASH_Unlock();
    status = HAL_FLASHEx_Erase(&eraseInit,&sectorErr);
    HAL_FLASH_Lock();
    return status;
}


    

